

#ifndef _POLLER_H_
#define _POLLER_H_

#include "Timestamp.hpp"
#include <unordered_map>
#include <vector>

namespace zephyr {

class Channel;
class EventLoop;

// 多路事件分发器
class Poller {
  public:
    using ChannelList = std::vector<Channel *>;
    Poller(EventLoop *loop);
    virtual ~Poller() = default;

    // IO复用统一接口
    /**
     * @param activeChannels [out]
     */
    virtual Timestamp poll(int timeoutMs, ChannelList *activeChannels) = 0;
    virtual void updateChannel(Channel *channel) = 0;
    virtual void removeChannel(Channel *channel) = 0;

    // 判断channel是否在当前poller中
    bool hasChannel(Channel *channel) const;

    static Poller *newDefaultPoller(EventLoop *loop);
  protected:
    using ChannelMap = std::unordered_map<int, Channel *>;
    ChannelMap m_channels;

  private:
    EventLoop *m_ownerloop;
};


}

#endif // _POLLER_H_